<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
    </el-row>
    <!--  @selection-change="handleSelectionChange" -->
    <el-table v-loading="loading" :data="detailList" height="600">
      <!-- <el-table-column type="selection" width="55" align="center" v-if="optType!=='check'" /> -->
      <el-table-column type="index" label="序号" align="center" width="50" />
      <el-table-column label="物料编码" align="center" prop="itemCode" />
      <el-table-column label="物料名称" align="center" prop="sonname" />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="数量" align="center" prop="itemAmount">
        <template slot-scope="scope">
          <el-input v-model="scope.row.itemAmount" placeholder="请输入数量" v-if="optType!=='check'"></el-input>
          <span v-if="optType==='check'">{{scope.row.itemAmount}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDelete(scope.row,scope.$index)"
            v-if="optType == 'add'"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="optType == 'update'"
            @click="handleDelete(scope.row,scope.$index)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <MaterialList ref="materialListRef" v-if="openList" @checkQuery="checkParam"></MaterialList>
  </div>
</template>

<script>
import {
  getItemListDetail,
  getDetail,
  delDetail,
  addDetail,
  updateDetail
} from "@/api/mes/pro/supplyDetail";
import { listMaterialInfo, getSupplyInfo } from "@/api/mes/pro/supply";
import MaterialList from "./materialsList.vue";

export default {
  name: "supplyDetail",
  props: ["optType", "parentId"],
  components: { MaterialList },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产补料详细信息表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //勾选的内容
      selectData: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialSerial: null,
        productCode: null,
        productName: null,
        productSpec: null,
        measureName: null,
        itemAmount: null,
        supplyId: this.parentId,
        suppyTime: null,
        workorderCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      openList: false
    };
  },
  created() {
  },
  methods: {
    init(row, optType) {
      this.form = row;
      this.optType = optType;
      this.getList();
    },
    /** 查询生产补料详细信息列表 */
    getList() {
      this.loading = false;
      if (this.optType != "add") {
        console.log(this.optType, this.form, "=========this.form======");
        getItemListDetail(this.queryParams).then(response => {
          this.detailList = response.rows;
          this.$emit("onSelected", this.detailList);
          this.total = response.total;
          this.loading = false;
        });
      }

      // if (this.optType == "add") {
      //   if (this.queryParams.workorderCode != null) {
      //     this.loading = true;
      //     listMaterialInfo(this.queryParams).then(response => {
      //       console.log(this.queryParams, "------------------------------");
      //       console.log(response.data, "------------------------------");
      //       this.detailList = response.data;
      //       // this.total = response.total;
      //       this.loading = false;
      //       // this.detailList = selection;
      //       // this.$emit("onSelected", this.detailList);
      //     });
      //   }
      // }
      // if (this.optType == "update" || this.optType == "check") {
      //   this.loading = true;
      //   listDetail(this.queryParams).then(response => {
      //     this.detailList = response.rows;
      //     this.total = response.total;
      //     this.loading = false;
      //   });
      // }
    },
    checkParam(list) {
      console.log(list, "==========l;ist===============");
      this.detailList = this.detailList.concat(list);
      this.$emit("onSelected", this.detailList);
      console.log(this.detailList, "==========l;ist===============");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.openList = false;
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   console.info("selection===", selection);
    //   this.ids = selection.map(item => item.id);
    //   this.single = selection.length !== 1;
    //   this.multiple = !selection.length;
    //   // this.detailList = selection;
    //   // this.$emit("onSelected", this.detailList);
    // },
    //删除
    handleDelete(row, index) {
      if (this.optType == "update") {
        let materialSerial = row.itemCode;
        let backId = this.queryParams.backId;
        let ids = [];
        ids.push(row.id);
        this.$modal
          .confirm(
            '是否确认删除物料编号为"' + materialSerial + '"的数据项？'
          ).then(() => {
            console.log("1111111111111",index);
            this.detailList.splice(index, 1);
            this.$modal.msgSuccess("删除成功");
            this.$emit("onSelected", this.detailList);
          })
          .catch(() => {});
      } else if (this.optType == "add") {
        this.detailList.splice(index, 1);
        this.$emit("onSelected", this.detailList);
      }
    },
    handleAdd() {
      console.log(this.form, "=======this.form=======");
      if (!!this.form.workorderCode) {
        this.openList = true;
        this.$nextTick(() => {
          this.$refs.materialListRef.init(this.form, this.detailList);
        });
      } else {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请先填写生产工单相关信息！！!"
        });
        return;
      }
    }
  }
};
</script>
